/**
 * @class   wosh::gui::WidgetTextMessage
 * @brief   Graphical interface of wosh::communication::TextMessage
 *          
 *          
 *
 ****************************************************************************
 * @version $Id: WidgetTextMessage.h 2184 2010-03-20 13:19:23Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Gui_WidgetTextMessage_H__
 #define __WOSH_Gui_WidgetTextMessage_H__

 #include <core/Notification.h>
 #include <framework/communication/TextMessage.h>

 #include <QtGui/QWidget>
 #include "ui_WidgetTextMessage.h"


namespace wosh {
 using namespace communication;
 namespace gui {


class WidgetTextMessage : public QWidget {

    Q_OBJECT

	public:
		WidgetTextMessage(QWidget *parent = 0) : QWidget(parent)
		 {
			this->m_ui = new Ui::WidgetTextMessage();
			this->m_ui->setupUi(this);

			this->notification = NULL;

			connect(this->m_ui->txtRecipent, SIGNAL(editingFinished()), this, SLOT(txtRecipent_editingFinished()));
			connect(this->m_ui->txtSubject, SIGNAL(editingFinished()), this, SLOT(txtSubject_editingFinished()));
			connect(this->m_ui->txtBody, SIGNAL(textChanged()), this, SLOT(txtBody_textChanged()));
			connect(this->m_ui->cmbType, SIGNAL(activated(const QString&)), this, SLOT(cmbType_activated(const QString&)));

		 }

		virtual ~WidgetTextMessage()
		 {
			delete this->m_ui;
			if ( this->notification != NULL )
				delete this->notification; this->notification = NULL;
		 }

		void loadNotification( const TextMessage* notif )
		 {
			if ( notif == NULL ) {
				this->m_ui->txtBody->setText( notif->getPlain().c_str() );

				return;
			 }

			this->m_ui->txtRecipent->setText( notif->getRecipent_User().c_str() );
			this->m_ui->txtBody->setText( this->notification->getPlain().c_str() );

		 }

		Notification* newNotification()
		 {
			if ( this->notification != NULL )
				delete this->notification;
			this->notification = new TextMessage();

			this->notification->setPlain( this->m_ui->txtBody->toPlainText().toLatin1().data() );

			return this->notification;
		 }

		Notification* getNotification( ) { return this->notification; }


	protected slots:

		void txtRecipent_editingFinished() {
			if ( this->notification == NULL ) return;
			this->notification->setRecipent_User( this->m_ui->txtRecipent->text().toLatin1().data() );
		 }
		void txtSubject_editingFinished() {
			if ( this->notification == NULL ) return;
			this->notification->setSubject( this->m_ui->txtSubject->text().toLatin1().data() );
		 }


		void txtBody_textChanged() {
			if ( this->notification == NULL ) return;
			this->notification->setPlain( this->m_ui->txtBody->toPlainText().toLatin1().data() );
		 }

		void cmbType_activated( const QString& ) {
			if ( this->notification == NULL ) return;

		 }

	protected:
		void changeEvent(QEvent *e) {
			QWidget::changeEvent(e);
			switch (e->type()) {
				case QEvent::LanguageChange:
					this->m_ui->retranslateUi(this);
					break;
				default:
					break;
			 }
		 }

	private:
		Ui::WidgetTextMessage *m_ui;

		TextMessage* notification;

}; // class def

 }; // namespace gui
}; // namespace wosh


#endif //__WOSH_Gui_WidgetTextMessage_H__
